package com.zhuizikejithreeig.cn.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhuizikejithreeig.cn.adapter.EmailActivtyAdapter;
import com.zhuizikejithreeig.cn.adapter.EmailRecycleAdapter;
import com.zhuizikejithreeig.cn.been.NoticeBeen;
import com.zhuizikejithreeig.cn.content.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;
import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by 赵航 on 2018/2/27.
 */

public class PagerFragemnt extends Fragment {

    private String             mTitle;
    private RecyclerView mRecyclerView;
    private TextView           mTextView;
    private SmartRefreshLayout mSmartRefreshLayout;

    private String         getData;
    private ProgressDialog mDialog;
    private NoticeBeen mBeen;
    private List<NoticeBeen.DataBean> mList         = new ArrayList<>();
    private List<NoticeBeen.DataBean> mActivityList = new ArrayList<>();
    private EmailRecycleAdapter mAdapter;
    private EmailActivtyAdapter mActivityAdapter;


    //这是单例模式,懒汉式
    public static PagerFragemnt getInstance(String title) {
        PagerFragemnt sf = new PagerFragemnt();
        sf.mTitle = title;

        return sf;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.pagerfragemnt, null);

        mSmartRefreshLayout = v.findViewById(R.id.smartrefreshlayout);
        mSmartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        //下拉刷新
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                mSmartRefreshLayout.finishRefresh();
            }
        });

        //上拉加载更多
        mSmartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mSmartRefreshLayout.finishLoadmore();
            }
        });
        mRecyclerView = v.findViewById(R.id.recycleview);
        mTextView = v.findViewById(R.id.text);


        if (Utils.netConnected(getContext())) {
            haveData(mTitle);
        }

        return v;
    }

    private void haveData(String title) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        if (TextUtils.equals(title, "1")) {
            getData = "1";
            //设置RecyclerVIew的管理器
            mAdapter = new EmailRecycleAdapter(getData, mList);
            mRecyclerView.setAdapter(mAdapter);
            mList.clear();
        } else {
            getData = "2";
            mActivityAdapter = new EmailActivtyAdapter(getContext(), getData, mActivityList);
            mRecyclerView.setAdapter(mActivityAdapter);
            mActivityList.clear();
        }


        mDialog = new ProgressDialog(getContext());
        //设置进度条不可取消
        mDialog.setCancelable(false);
        mDialog.setMessage("加载中,请稍等.....");
        mDialog.show();
        initData(getData);
    }

    private void initData(final String getData) {

        OkHttpUtils
                .post()
                .url(UriContect.NEWS)
                .addParams("type", getData)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mDialog.dismiss();
                        ToastMake.showToast(getResources().getString(R.string.string58));
                        isNoData();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mDialog.dismiss();
                        if (TextUtils.equals(getData, "1")) {

                            mBeen = GsonUtil.parseJsonToBean(response, NoticeBeen.class);
                            if (mBeen == null) {
                                isNoData();
                                return;
                            }

                            if (!TextUtils.equals(mBeen.getStatus(), "200")) {
                                ToastMake.showToast(mBeen.getMsg());
                                isNoData();
                                return;
                            } else {
                                //获取数据成功
                                List<NoticeBeen.DataBean> list = mBeen.getData();
                                if (list.size() == 0) {
                                    isNoData();
                                } else {
                                    mList.addAll(list);
                                    mAdapter.notifyDataSetChanged();
                                }

                            }
                        } else {


                            mBeen = GsonUtil.parseJsonToBean(response, NoticeBeen.class);
                            if (mBeen == null) {
                                isNoData();
                                return;
                            }
                            if (!TextUtils.equals(mBeen.getStatus(), "200")) {
                                ToastMake.showToast(mBeen.getMsg());
                                isNoData();
                                return;
                            } else {
                                //获取数据成功
                                List<NoticeBeen.DataBean> list = mBeen.getData();
                                if (list.size() == 0) {
                                    isNoData();
                                } else {
                                    mActivityList.addAll(list);
                                    mActivityAdapter.notifyDataSetChanged();
                                }

                            }


                        }

                    }
                });

    }

    /**
     * @tab 判断listview是否有值, 进行界面切换
     */

    private void isNoData() {
        mRecyclerView.setVisibility(View.GONE);
        mTextView.setVisibility(View.VISIBLE);
    }

}
